#include <common.h>
void do_syscall(Context *c);
enum {
  MSATUS_MIE_MASK = 1 << 3,
  MSATUS_MPIE_MASK = 1 << 7,
  MSATUS_MPP_MASK = 3 << 11,
  MIE_MTIE_MASK = 1 << 7,
  MIP_MTIP_MASK = 1 << 7,
  MIP_MSIP_MASK = 1 << 3,
};
static Context* do_event(Event e, Context* c) {
  switch (e.event) {
    case EVENT_SYSCALL: do_syscall(c); break;
    case EVENT_YIELD: Log("EVENT_YIELD");  break;
    case EVENT_IRQ_TIMER: 
      uint64_t mtip = 0;//"r"(mtip)
      asm volatile("csrrc %0,mip,%0" : "=r"(mtip)   : "r"(MIP_MTIP_MASK));
      if(mtip)
       Log("EVENT_IRQ_TIMER:mtip:0x%x",mtip); 
      else Log("EVENT_IRQ_TIMER:error");
      
      break;
    default: panic("Unhandled event ID = %d", e.event); break;
  }
  return c;
}

void init_irq(void) {
  Log("Initializing interrupt/exception handler...");
  cte_init(do_event);
    // asm volatile("csrs mstatus, %0" : : "r"(MSATUS_MIE_MASK));
    // asm volatile("csrs mie, %0" : : "r"(MIE_MTIE_MASK));

}
